package com.eascs.web.api.view.method;

import java.lang.reflect.Type;
import java.util.List;

import com.eascs.common.util.ClassUtil;
import com.eascs.web.api.ApiMethodInvokeObject;

public class ApiMethodInvokeObjectView extends ApiMethodInvokeObjectSimpleView {

	private List<ApiParam> requestApiParams;
	private List<ApiParam> responseApiParams;

	public List<ApiParam> getRequestApiParams() {
		return requestApiParams;
	}

	public void setRequestApiParams(List<ApiParam> requestApiParams) {
		this.requestApiParams = requestApiParams;
	}

	public List<ApiParam> getResponseApiParams() {
		return responseApiParams;
	}

	public void setResponseApiParams(List<ApiParam> responseApiParams) {
		this.responseApiParams = responseApiParams;
	}

	public ApiMethodInvokeObjectView(ApiMethodInvokeObject apiMethodInvokeObject) {
		super(apiMethodInvokeObject);
		buildRequestOptions(apiMethodInvokeObject);
		buildResponseOptions(apiMethodInvokeObject);
	}

	private void buildResponseOptions(ApiMethodInvokeObject apiMethodInvokeObject) {
		if (ClassUtil.isSimpaleType(apiMethodInvokeObject.getMethod().getReturnType()))
			return;
		Type responseType = apiMethodInvokeObject.getMethod().getGenericReturnType();
		this.responseApiParams = ApiParamBuilder.buildApiParams(responseType);
	}

	private void buildRequestOptions(ApiMethodInvokeObject apiMethodInvokeObject) {
		if (apiMethodInvokeObject.getMethod().getGenericParameterTypes().length == 0)
			return;
		if (ClassUtil.isSimpaleType(apiMethodInvokeObject.getMethod().getParameterTypes()[0]))
			return;

		Type requestType = apiMethodInvokeObject.getMethod().getGenericParameterTypes()[0];
		this.requestApiParams = ApiParamBuilder.buildApiParams(requestType);
	}

}
